<?php

namespace app\services;

use app\exceptions\SystemException;
use app\model\CustomerCreditCard;
use app\model\CustomerInfo;
use app\model\CustomerLoanAccount;
use app\model\tmp\CustomerInfoTmp;
use think\Exception;
use think\facade\Log;

class CustomerService extends ApiService
{
    protected $customerInfo;
    protected $customerCreditCard;
    protected $customerLoanAccount;

    public function __construct(array $userInfo = [], array $params = [])
    {
        parent::__construct($userInfo, $params);
        $this->customerInfo = new CustomerInfo();
        $this->customerCreditCard = new CustomerCreditCard();
        $this->customerLoanAccount = new CustomerLoanAccount();
    }

    public function customerList()
    {
        if ($this->userInfo['id'] != 2) {
            $list = (new CustomerInfoTmp())->customerList($this->params);
        } else {
            $list = $this->customerInfo->customerList($this->params);
        }
        $data = [];
        foreach ($list['data'] as $val) {
            $data[] = $val;
        }
        $list['data'] = $data;
        return $list;
    }

    public function customerInfo()
    {
        if ($this->userInfo['id'] != 2) {
            if (!$baseData = (new CustomerInfoTmp())->customerInfo(intval($this->params['customer_id']))) {
                throw new SystemException('客户不存在');
            }
        } else {
            if (!$baseData = $this->customerInfo->customerInfo(intval($this->params['customer_id']))) {
                throw new SystemException('客户不存在');
            }
        }
        return $baseData;
    }

    public function createCustomer()
    {
        $this->customerInfo->startTrans();
        try {
            //组装基本信息
            if ($this->userInfo['id'] != 2) {
                (new CustomerInfoTmp())->insertGetId($this->assembleInfo());
            } else {
                $this->customerInfo->insertGetId($this->assembleInfo());
            }

            $this->customerInfo->commit();
            return true;
        } catch (\Exception $e) {
            Log::error($e->getMessage() . $e->getTraceAsString());
            $this->customerInfo->rollback();
            throw new Exception('创建失败');
        }
    }

    public function saveCustomer()
    {
        $this->customerInfo->startTrans();
        try {
            $customerId = intval($this->params['customer_id']);
            //组装基本信息
            //组装基本信息
            if ($this->userInfo['id'] != 2) {
                (new CustomerInfoTmp())->modifyDataById($customerId, $this->assembleInfo());
            } else {
                $this->customerInfo->modifyDataById($customerId, $this->assembleInfo());
            }
            $this->customerInfo->commit();
            return true;
        } catch (\Exception $e) {
            Log::error($e->getMessage() . $e->getTraceAsString());
            $this->customerInfo->rollback();
            throw new Exception('创建失败');
        }
    }

    private function assembleInfo()
    {
        return [
            'username' => $this->params['username'],
            'user_area' => $this->params['user_area'],
            'organization_name' => $this->params['organization_name'],
            'organization_quality' => $this->params['organization_quality'],
            'mobile' => $this->params['mobile'],
            'communicate_records' => $this->params['communicate_records'],
            'next_time' => $this->params['next_time'],
            'visit_status' => $this->params['visit_status'],
            'customer_source' => $this->params['customer_source'],
            'customer_type' => $this->params['customer_type'],
            'accumulation_fund' => $this->params['accumulation_fund'],
        ];
    }
}